
import { MoreWindow } from '../../type'
/**
 * @public 将数字换成中文汉字
 */

export const toChinesNum = (num:number|string):number|string => {
    const paramNum = num;
    num = Number(num);
    if(!isNaN(num)){
        let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; 
        let unit = ["", "十", "百", "千", "万"];
        num = parseInt(num.toString());
        let getWan = (temp) => {
            let strArr = temp.toString().split("").reverse();
            let newNum = "";
            for (var i = 0; i < strArr.length; i++) {
                newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
            }
            return newNum;
        };
        let overWan = Math.floor(num / 10000);
        let noWan:string|number = num % 10000;
        if (noWan.toString().length < 4) {
            noWan = "0" + noWan;
        }
        return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
    }else{
        return paramNum;
    }
}

export const getDeviceInfo=(key:string):any=>{
    let agent = navigator.userAgent.toLowerCase();
    let win:MoreWindow = window;
    console.log(agent);
    function getVersion(label){
        let exp = new RegExp(label+'/([^\\s\\_\\-]+)');
        label = (agent.match(exp)||[])[1];
        return label||false;
    }
    let result:any = {
        os:function(){
            if(/windows/.test(agent)){
                return 'windows';
            } else if(/linux/.test(agent)){
                return 'linux';
            } else if(/iphone|ipod|ipad|ios/.test(agent)){
                return 'ios';
            } else if(/mac/.test(agent)){
                return 'mac';
            } 
        }(),
        ie:function(){
            return (!!win.ActiveXOject||"ActiveXObject" in win)?(
                (agent.match(/msie\s(\d+)/)||[])[1] || '11'
            ):false;
        }(),
        weixin:getVersion('micromessenger') //是否微信
    };
    //任意的key
    if(key&&!result[key]){
        result[key] = getVersion(key);
    };
    //移动设备
    result.android = /android/.test(agent);
    result.ios = result.os === 'ios';
    result.mobile = (result.android||result.ios)?true:false;
    return result;
}

// 根据指定名称获取cookie
export const getCookieByName=(name:string):string=>{
    return `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();
};

// 颜色RGB转十六进制
export const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

// 检查日期是否合法
export const isDateValid = (...val:[any]) => !Number.isNaN(new Date(...val).valueOf());

// 英文字符串首字母大写
export const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);

// 获取svg dom 节点的xml
export const getSvgDomXml =(dom:Element):string=>{
    let svgAsXML = new XMLSerializer().serializeToString(dom);
    return svgAsXML;
};

// 获取svg dom 节点的base64
export const getSvgDomXmlBase64 =(dom:Element):string=>{
    let svgAsXML = new XMLSerializer().serializeToString(dom);
    let str = 'data:image/svg+xml,' + encodeURIComponent(svgAsXML);
    return str;
};